<?php
namespace app\blogadmin\Controller;

use app\blogadmin\model\Admin;
use think\captcha\Captcha;
use think\Controller;
use think\Db;
use think\Request;
use think\Session;



class Login extends Controller
{
    /**
     * 空方法，防止用户乱输入url
     */
    public function _empty(){
        $this->redirect('Login/login');
    }

    /**
     * 登录
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function login()
    {
        if (Request::instance()->isPost()) {
            //获取传递过来的值
            $data =input('post.');
            if(!captcha_check($data['verity'])){
                return json(['code'=>'-1','msg'=>'验证码错误，请重新输入']);
            }
            $log_info['username']=htmlspecialchars($data['username']);
            $log_info['password']=htmlspecialchars(md5($data['password']));
            $adminM = new Admin();
            $res = $adminM->getLoginMember($log_info);      //查找当前登陆用户信息
            if($res['code']=='1'){
                $log_info['last_ip']=Request::instance()->ip();
                $log_info['login_time']=time();
                $log_info['id']=$res['res']['id'];
                if ($res['res']['status'] == 1) {                      //正常状态,是否被锁定
                    if($res['res']['username']==$data['username'] && $res['res']['password']==md5($data['password'])){ //判断用户和密码是否正确
                        $adminM->upda($log_info);              //存入登陆信息
                        //保存session
                        $admin['id']=$res['res']['id'];
                        $admin['username']=$res['res']['username'];
                        $admin['role_id']=$res['res']['role_id'];
                        $admin['phone']=$res['res']['phone'];
                        $admin['email']=$res['res']['email'];
                        $admin['last_ip']=$res['res']['last_ip'];
                        $admin['login_time']=$res['res']['login_time'];
                        $admin['status']=$res['res']['status'];
                        //保存用户数据
                        Session::set('admin',$admin);
                        return json(['code'=>'1','msg'=>'登陆成功']);
                    }else{
                        return json(['code'=>'-1','msg'=>'用户名或密码错误']);
                    }
                } else {
                    return json(['code'=>'-1','msg'=>'对不起，您已经被锁定']);
                }
            }else{
                return json(['code'=>'-1','msg'=>'用户名或密码错误']);
            }
        }
        return view();
    }

    public function edit(){
        $id=Request::instance()->param('id');//获取用户id
        $adminM=new  admin();
        $cur_admin=$adminM->getOne(['id'=>$id]); //获取当前用户信息

        //获取提交过来的信息
        if(Request::instance()->isPost()){
            $data=input('post.');
            unset($data['repassword']);
            $data['password']=md5($data['password']);
            //修改相应的数据
            $res=Admin::update($data);
            if($res){
                return ['code'=>'1','msg'=>'修改密码成功'];
            }else{
                return ['code'=>'-1','msg'=>'修改密码失败'];
            }


        }

        $this->assign('cur_admin',$cur_admin);
        return $this->fetch();
    }

    /**
     * 退出登陆
     */
    public function logout(){
        //清除session值
        Session::delete('admin');
        $this->redirect('login/login');
    }





}
